import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * EBook Organizer
 * 4/30/2010
 * <P>
 * The model that contains all the settings.
 * </P>
 * @version 0.1
 */
public class SettingsModel extends Settings implements java.io.Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7503086199401682499L;
	
	private ArrayList<ActionListener> actionListenerList;
	
	public SettingsModel() {
		super();
		processEvent(
				new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "start") );
	}
	
	public void addFileType( FileType type) {
		super.addFileType(type);
		processEvent(
				new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "add File Type") );
	}
	
	public void removeFileType (FileType type) {
		super.removeFileType( type );
		processEvent(
				new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "remove File Type") );
	}
	
	public void setFilePathName(String path) {
		super.setFilePathName(path);
		processEvent(
				new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "set Path Name") );
	}
	
	public synchronized void addActionListener(ActionListener l) {
		if (actionListenerList == null) {
			actionListenerList = new ArrayList<ActionListener>();
		}
		
		actionListenerList.add(l);
	}
	
	public synchronized void removeActionListener(ActionListener l) {
		if (actionListenerList != null && actionListenerList.contains(l))
			actionListenerList.remove(l);
	}
	
	@SuppressWarnings("unchecked")
	public void processEvent(ActionEvent e) {
		ArrayList<ActionListener> list;
		
		synchronized(this) {
			if (actionListenerList == null) return;
			list = (ArrayList<ActionListener>)actionListenerList.clone();
		}
		
		for (int i = 0; i < list.size(); i++) {
			ActionListener listener = list.get(i);
			listener.actionPerformed(e);
		}
		
		System.out.println(e.paramString());	
		}
	
}
